Minimum Spanning Tree
We define a saft edge for a subset of some minimum spanning tree if adding it to still is a part of a minimum spanning tree.
- We define a cut (S, V - S) of an undirected graph is a partition of .
- We say a edge crosses a cut (S, V - S) if and .
- We say that a cut respects a set of edges if no edge in crosses the cut.
- An edge is light edge crossing a cut (S, V - S) if its weight is the minimum weight of all edges crossing the cut.
Let be a connected undirected graph with a real-valued weight function defined on . Let EG(S, V-S)GA(u,v)(S, V-S)(u,v)A$.
Let be a connected, undirected graph with a real-valued weight function defined on . Let be a subset of that is included in some minimum spanning tree for , and let be a connected component (tree) in the forest . If is a light edge connecting to some other component in , then is safe for .
Kruskal's Algorithm
Kruskal’s algorithm finds a safe edge to add to the growing forest by finding, of all the edges that connect any two trees in the forest, an edge of least weight. One implementation of Kruskal’s algorithm is to use a disjoint-set data structure to keep track if two vertices are in the same tree.
The basic idea:
- Make a set for each vertex.
- sort the edges in nondecreasing order of weight.
- Union sorted edges if they are not in the same set.
- return the set of edges that are in the same set.
The running time of Kruskal’s algorithm is .
Prim's Algorithm
Prim’s algorithm finds a safe edge to add to the growing forest by finding, of all the edges that connect any vertex in the forest to any vertex not in the forest, an edge of least weight. One implementation of Prim’s algorithm is to use a priority queue to keep track of the light edges that connect the forest to the rest of the graph.
The basic idea:
- set up all keys and parents.
- set up a min-prority heap by vertex.
- set root key to 0 and then do while loop
- modify the adj vertices' keys and parents by the light edges.
The running time of Prim’s algorithm is .